package com.kitty;

import com.kitty.common.core.SchedulerManager;
import com.kitty.common.utils.SpringUtils;
import com.kitty.common.utils.ToolsPub;
import com.kitty.game.market.MarketService;
import com.kitty.game.merge.MergeService;
import com.kitty.game.player.PlayerService;
import com.kitty.game.zhenbao.ZhenbaoService;
import com.kitty.mina.session.SessionManager;
import com.kitty.mina.task.TaskHandlerContext;
import org.apache.commons.lang3.time.StopWatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.Banner;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.scheduling.annotation.EnableScheduling;

/**
 * 启动程序
 * 
 * @author wanfeng
 */

@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
@EnableScheduling
@EnableCaching
public class WanFengApplication  extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application)
    {
        return application.sources(WanFengApplication.class);
    }


    private static Logger logger = LoggerFactory.getLogger(WanFengApplication.class);
    public static void main(String[] args)
    {
        System.setProperty("spring.devtools.restart.enabled", "false");
        int b=9966390;
        int a =((( b & 0xFF00) >> 8) ^ b )& 0xFFFF;


        logger.warn("启动成功，耗時{}毫秒",  a );


        //eturn

        StopWatch stopWatch = new StopWatch();

        stopWatch.start();

        try {

            SpringApplicationBuilder applicationBuilder = new SpringApplicationBuilder(WanFengApplication.class);
            applicationBuilder.bannerMode(Banner.Mode.OFF);/**关闭Banner*/
            applicationBuilder.run(args);
            // id生成器
            SpringUtils.getBean(ToolsPub.class).getResFile("gongming.json");
            // id生成器
            SpringUtils.getIdentityService().init();

            SpringUtils.getBean(PlayerService.class).loadAllPlayerProfiles();


            SpringUtils.getBean(MarketService.class).init();

            SpringUtils.getBean(ZhenbaoService.class).init();
            SpringUtils.getBean(MergeService.class).init();
//            SpringUtils.getBean(RedisDao.class).flushDB();
        } catch (Exception e) {
            logger.info("启动异常：{}", e);
            System.exit(-1);
        }

        stopWatch.stop();

        logger.info("启动成功，耗時{}毫秒", stopWatch.getTime());

        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                onServerShutDown();
            }
        });
    }

    private static void onServerShutDown() {
        logger.info("服务准备关闭");
        try {
            // 踢人下线
            SessionManager.INSTANCE.killAllPlayers();
        } catch (Exception e) {
            //  LoggerUtils.error("", e);
        }
        TaskHandlerContext.INSTANCE.shutDown();
        SchedulerManager.getInstance().shutDown();

        logger.error("服务准备关闭结束");
    }
}